﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.BaseInfo;
using ZHDJ.Data.Models.Questionnaire;

namespace SysManage.Module.Questionnaire
{
    public partial class PartyQuestionnaireEdit : PageBase
    {
        #region ViewPower
        public string id { get { return GetQueryIntValue("id"); } }
        public string Module { get { return GetQueryIntValue("Module"); } }
        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyQuestionnaireAdd";
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {

            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyQuestionnaire current = DB.PartyQuestionnaires.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                txtTitle.Text = current.Title;
                dpBeginTime.Text = Convert.ToDateTime(current.BeginTime).ToString("yyyy-MM-dd");
                dpEndTime.Text = Convert.ToDateTime(current.EndTime).ToString("yyyy-MM-dd");
                cbxEnabled.Checked = current.Stauts == 4;
                ddlTestObject.SelectedValue = current.TestObject;
            }
          
        }

        #endregion

        #region Events

        private void SaveData()
        {

            string id = GetQueryIntValue("id");
            PartyQuestionnaire item;
            if (id != "")
            {
                //更新
                item = DB.PartyQuestionnaires.Find(id);
                if (item == null)
                {
                    Alert.Show("参数错误！", String.Empty);
                    return;
                }

                item.ModifyTime = DateTime.Now;
                item.ModifyBy = CurrentUser.ID;
            }
            else
            {
                item = new PartyQuestionnaire();
                item.ID = StringHelper.NewID;

                item.CreateTime = DateTime.Now;
                item.CreateBy = CurrentUser.ID;
                item.ModifyTime = DateTime.Now;
                item.ModifyBy = CurrentUser.ID;
                DB.PartyQuestionnaires.Add(item);

            }
            item.Title = txtTitle.Text;
            item.BeginTime = Convert.ToDateTime(dpBeginTime.Text);
            item.EndTime = Convert.ToDateTime(dpEndTime.Text);

            item.Stauts = cbxEnabled.Checked ? 4 : 1;
            item.TestObject = ddlTestObject.SelectedValue;
            DB.SaveChanges();
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            //Alert.Show("添加成功！", String.Empty, ActiveWindow.GetHidePostBackReference());
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

    }
}

